package com.yanqu.road.dao.impl.activity.summergames;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesConvertInfo;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesLotteryInfo;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesProjectInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SummerGamesConfigDaoImpl extends TempDao {

    private static String getSummerGamesLotteryInfoSql = "select * from `t_s_activity_summergame_lottery` where `activityId`=? ORDER BY `id` ASC";
    public List<SummerGamesLotteryInfo> getSummerGamesLotteryInfoList(int activityId) {
        List<SummerGamesLotteryInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSummerGamesLotteryInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SummerGamesLotteryInfo data = new SummerGamesLotteryInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setWeight(rs.getInt("weight"));
                data.setLeastNum(rs.getString("leastNum"));
                data.setGuaranteesNum(rs.getString("guaranteesNum"));
                data.setScore(rs.getInt("score"));
                data.setName(rs.getString("name"));
                data.setMedalsType(rs.getInt("medalsType"));

                if(!StringUtils.isNullOrEmpty(data.getLeastNum()) &&
                        !"0".equals(data.getLeastNum()) && !"0;0".equals(data.getLeastNum())){
                    data.setLeastNumMap(StringUtils.stringToIntegerList(data.getLeastNum(), "\\|"));
                }
                if(!StringUtils.isNullOrEmpty(data.getGuaranteesNum()) &&
                        !"0".equals(data.getGuaranteesNum()) && !"0;0".equals(data.getGuaranteesNum())){
                    data.setNormalNumMap(StringUtils.stringToIntegerList(data.getGuaranteesNum(), "\\|"));
                }

                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getSummerGamesProjectInfoSql = "select * from `t_s_activity_summergame_project` where `activityId`=? ORDER BY `id` ASC";
    public List<SummerGamesProjectInfo> getSummerGamesProjectInfoList(int activityId) {
        List<SummerGamesProjectInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSummerGamesProjectInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SummerGamesProjectInfo data = new SummerGamesProjectInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setProjectName(rs.getString("projectName"));
                data.setVenuesId(rs.getInt("venuesId"));
                data.setVenuesName(rs.getString("venuesName"));
                data.setProjectDesc(rs.getString("projectDesc"));
                data.setMedalsId(StringUtils.stringToIntegerList(rs.getString("medalsId"), "\\|"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getSummerGamesConvertInfoSql = "select * from `t_s_activity_summergame_convert` where `activityId`=? ORDER BY `id` ASC";
    public List<SummerGamesConvertInfo> getSummerGamesConvertInfoList(int activityId) {
        List<SummerGamesConvertInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSummerGamesConvertInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SummerGamesConvertInfo data = new SummerGamesConvertInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setVenuesId(rs.getInt("venuesId"));
                data.setExchangeReward(rs.getString("exchangeReward"));
                data.setExchangeItem(rs.getString("exchangeItem"));
                data.setNumLimit(rs.getInt("numLimit"));
                data.setType(rs.getInt("type"));
                data.setExchangeItemList(StringUtils.stringToIntegerList(data.getExchangeItem(), "\\|"));

                String flexiExchangeReward = rs.getString("flexiExchangeReward");
                if(!StringUtils.isNullOrEmpty(flexiExchangeReward) && !"0".equals(flexiExchangeReward)){
                    List<String> flexiExchangeList = StringUtils.stringToStringList(flexiExchangeReward, ";");
                    for (String flexiExchange : flexiExchangeList) {
                        List<String> stringList = StringUtils.stringToStringList(flexiExchange, ",");
                        data.getFlexiExchangeRewardMap().put(stringList.get(0), Integer.parseInt(stringList.get(1)));
                    }
                }
                data.setFlexiExchangeReward(flexiExchangeReward);
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
